package com.hwhhhh.mygrad.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentTransaction;

import com.bumptech.glide.Glide;
import com.hwhhhh.mygrad.BaseActivity;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.UserInfoActivity;
import com.hwhhhh.mygrad.dto.UserDTO;
import com.hwhhhh.mygrad.utils.StringUtils;
import com.hwhhhh.mygrad.view.RoundImageView;

public class UserInfoFragment extends BaseFragment implements View.OnClickListener, HintDialogFragment.DialogFragmentCallback{
    private static final int REQUEST_CODE_PHOTO = 101;

    private ConstraintLayout headPortraitLayout, nameLayout, targetLayout, sexLayout;
    private TextView name, target, sex, tvHeader, tvSave;
    private RoundImageView headPortrait;
    private UserDTO user;

    @Override
    protected int initLayout() {
        return R.layout.fragment_user_info;
    }

    @Override
    protected void initView() {
        headPortraitLayout = mRootView.findViewById(R.id.head_portrait_layout);
        nameLayout = mRootView.findViewById(R.id.name_layout);
        targetLayout = mRootView.findViewById(R.id.target_layout);
        sexLayout = mRootView.findViewById(R.id.sex_layout);
        tvHeader = getActivity().findViewById(R.id.tv_header);
        tvSave = getActivity().findViewById(R.id.tv_save);

        name = mRootView.findViewById(R.id.tv_name);
        target = mRootView.findViewById(R.id.tv_target);
        sex = mRootView.findViewById(R.id.tv_sex);
        headPortrait = mRootView.findViewById(R.id.iv_head_portrait);
    }

    @Override
    protected void initData() {
        headPortraitLayout.setOnClickListener(this);
        nameLayout.setOnClickListener(this);
        targetLayout.setOnClickListener(this);
        sexLayout.setOnClickListener(this);
        // 设置标题
        tvHeader.setText("个人资料");
        // 保存键
        if (tvSave.getVisibility() == View.VISIBLE) tvSave.setVisibility(View.INVISIBLE);

        user = StringUtils.fromJson(getStringFromSp("user"), UserDTO.class);

        name.setText(user.getName());
        target.setText(user.getTarget());
        sex.setText("男");
        Glide.with(this).load(user.getProfilePhoto()).into(headPortrait);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.head_portrait_layout) {
            ((UserInfoActivity)getActivity()).checkCameraPermission();
            ((UserInfoActivity)getActivity()).checkStoragePermission();
            Intent pickIn = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            pickIn.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            getActivity().startActivityForResult(pickIn, REQUEST_CODE_PHOTO);
        } else if (id == R.id.name_layout) {
            ((UserInfoActivity)getActivity()).enterOtherFragment(new UserNameFragment());
        } else if (id == R.id.target_layout) {
            ((UserInfoActivity)getActivity()).enterOtherFragment(new UserTargetFragment());
        } else if (id == R.id.sex_layout) {

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PHOTO) {
            if (data == null) return;
            Uri uri = data.getData();
            showToast("相册" + uri);
        }
    }

    @Override
    public void doPositiveClick(int requestCode) {
        String permission = ((UserInfoActivity)getActivity()).getPermissionString(requestCode);
        if (!StringUtils.isEmpty(permission)){
            if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{permission},
                        requestCode);
            }else{
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
                startActivity(intent);
            }
        }
    }

    @Override
    public void doNegativeClick(int requestCode) {

    }

}
